<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="类别类型" prop="dicnarsType">
                <el-select v-model="queryParams.dicnarsType" disabled size="small" placeholder="码值类别类型">
                    <el-option
                            v-for="dicnars in dicnarsTypes"
                            :key="dicnars.dicnarsType"
                            :label="dicnars.dicnarsName"
                            :value="dicnars.dicnarsType"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="码值名称" prop="dataName">
                <el-input
                        v-model="queryParams.dataName"
                        placeholder="请输入码值名称"
                        clearable
                        style="width: 240px;"
                        size="small"/>
            </el-form-item>
            <el-form-item label="码值类型" prop="dataType">
                <el-input
                        v-model="queryParams.dataType"
                        placeholder="请输入码值类型"
                        clearable
                        style="width: 240px;"
                        size="small"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="getList"
                           v-has="'basic:dictionariesdata:page'">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        size="small"
                        icon="el-icon-plus"
                        @click="save(null)"
                        v-has="'basic:dictionariesdata:add'"
                >新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button
                        type="primary"
                        plain
                        icon="el-icon-delete"
                        @click="remove"
                        v-has="'basic:dictionariesdata:delete'"
                >删除
                </el-button>
            </el-col>
        </el-row>
        <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55"/>
            <el-table-column label="码值表id" align="center" prop="id" v-if="false"/>
            <el-table-column label="码值名称" align="center" prop="dataName"/>
            <el-table-column label="码值类型" align="center" prop="dataType"/>
            <el-table-column label="状态" align="center" prop="state" :formatter="dicdataFormate"/>
            <el-table-column label="顺序" align="center" prop="dataSort"/>
            <el-table-column label="更新时间" align="center" prop="updateTime"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="save(scope.row)"
                            v-has="'basic:dictionariesdata:update'"
                    >修改
                    </el-button>
                    <el-button

                            type="text"
                            icon="el-icon-delete"
                            @click="remove(scope.row)"
                            v-has="'basic:dictionariesdata:delete'"
                    >删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryParams.pageNum"
                :page-sizes="sizes"
                :page-size="queryParams.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>


        <!-- 添加或修改参数配置对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form :model="form" :rules="rules" ref="form" class="login-form" label-width="80px">
                <el-form-item label="码值类别" prop="dicnarsType">
                    <el-input v-model="queryParams.dicnarsType" disabled/>
                </el-form-item>
                <el-form-item label="码值类型" prop="dataType">
                    <el-input v-model="form.dataType"/>
                </el-form-item>
                <el-form-item label="码值名称" prop="dataName">
                    <el-input v-model="form.dataName"/>
                </el-form-item>
                <el-form-item label="排序" prop="dataSort">
                    <el-input-number v-model="form.dataSort" :min="1" :max="99"></el-input-number>
                </el-form-item>
                <el-form-item label="状态" prop="state">
                    <el-radio-group v-model="form.state">
                        <el-radio v-for="datastate in dataStatesOptions"
                                  :key="datastate.dataType"
                                  :label="datastate.dataType">
                            {{datastate.dataName}}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" v-model="form.remark"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="close">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {listDictionaries} from "@/api/user/dictionaries";
    import {dicdatalist, getDicdata, addDicdata, delDicdata, updateDicdata} from "@/api/user/dicdata";

    export default {
        name: "dicData",
        data() {
            return {
                title: '查看详情',
                open: false,
                // 遮罩层
                loading: false,
                //码值类型
                dicnarsTypes: [],
                //状态字典
                dataStatesOptions: [],
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 1,
                //每页多少条数据，总共氛围多少个数组
                sizes: [10],
                // 表格数据
                list: [],
                ids: [],
                // 表单参数
                form: {
                    dicnarsId: null,
                    dataSort: 1,
                    id: null,
                    state: '1',
                },
                // 日期范围
                dateRange: [],
                // 查询参数
                queryParams: {
                    dicnarsType: null,
                    pageNum: 1,
                    pageSize: 10,
                    dataType:null,
                    dataName:null,
                },
                rules: {
                    dataType: [
                        {required: true, message: '请输入码值类型', trigger: 'blur'},
                        {max: 32, message: '长度在 32 个字符', trigger: 'blur'}
                    ],
                    dataName: [
                        {required: true, message: '请输入码值名称', trigger: 'blur'},
                        {max: 32, message: '长度在 32 个字符', trigger: 'blur'}
                    ]
                },
            };
        },
        created() {
            this.queryParams.dicnarsType = this.$route.query.type;
            this.form.dicnarsId = this.$route.query.id;
            this.queryParams.dicnarsId = this.$route.query.id;
            this.getList();
            this.listTypes();
            this.getDicdataByType("stats_data").then(response => {
                this.dataStatesOptions = response.data;
            });
        },
        methods: {
            handleSizeChange(val) {
                this.queryParams.pageSize = val;
                this.getList();
            },
            listTypes() {
                listDictionaries().then(response => {
                        this.dicnarsTypes = response.data;
                    }
                );
            },
            /**
             * 自定义组件，选择翻页时触发该组件
             * */
            handleCurrentChange(val) {
                this.queryParams.pageNum = val;
                this.getList();

            },
            /** 查询码值类型列表 */
            getList() {
                this.loading = true;
                dicdatalist(this.queryParams).then(response => {
                        this.list = response.data.records;
                        this.total = response.data.total;
                        let size = this.total / this.queryParams.pageSize;
                        for (let i = 1; i < size; i++) {
                            this.$set(this.sizes, i, (i + 1) * this.queryParams.pageSize);
                        }
                        this.loading = false;
                    }
                );
            },
            /**
             * 状态字段格式化
             * */
            dicdataFormate(row) {
                return this.selectLabel(this.dataStatesOptions, row.state);
            },
            /**
             * 打开新增或者修改界面
             * */
            save(row) {
                this.reset();
                if (row != null) {
                    getDicdata(row.id).then(response => {
                        this.title = '修改码值数据';
                        this.open = true;
                        this.form = response.data;
                    });
                } else {
                    this.title = '新增码值数据';
                    this.open = true;
                }
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.queryParams.pageNum = 1;
            },
            /**
             * 提交按钮
             * */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id == null || this.form.id == undefined) {
                            addDicdata(this.form).then(response => {
                                if (response.code == 0) {
                                    this.msgSuccess("新增成功成功");
                                    this.open = false;
                                    this.getList();
                                } else {
                                    this.msgError(response.msg);
                                }
                            })
                        } else {
                            updateDicdata(this.form).then(response => {
                                if (response.code == 0) {
                                    this.msgSuccess("修改成功");
                                    this.open = false;
                                    this.getList();
                                } else {
                                    this.msgError(response.msg);
                                }
                            })
                        }
                    }
                });
            },
            /**
             * 关闭弹出框
             * */
            close() {
                this.open = false;
                this.reset();
            },
            /**
             * 重置新增框中的东西
             * */
            reset() {
                // 表单参数
                this.form = {
                    dataSort: 1,
                    id: null,
                    state: '1',
                    dataType: null,
                    dataName: null,
                    remark: null,
                    dicnarsId: this.$route.query.id
                };
                this.resetForm("form");
            },
            /**
             * 多选框按钮，选中后将对应id记录下来
             * */
            handleSelectionChange(val) {
                this.ids = val.map(item => item.id);
            },
            /**
             * 删除数据
             * @param row
             */
            remove(row) {
                const ids = row.id || this.ids;
                delDicdata(ids).then(response => {
                    if (response.code == 0) {
                        this.msgSuccess("删除成功");
                        this.getList();
                    } else {
                        this.msgError(response.msg);
                    }
                });
            }
        }
    };
</script>

<style scoped>

</style>